package com.mxnet.chongjin.supoman.style;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

/**
 * Created by chongjin on 2017/12/14.
 */

public class MenuViewItem extends FrameLayout {
    private int width = -1;
    private int height = -1;
    private Bitmap bitmap;
    private int screenMode = 0;

    public MenuViewItem(Context context) {
        super(context);
    }

    public MenuViewItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MenuViewItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action != MotionEvent.ACTION_DOWN) {
            return super.onTouchEvent(event);
        }
        int x = (int) event.getX();
        int y = (int) event.getY();
        if (width == -1 || height == -1) {
            //            Drawable drawable = ((StateListDrawable)getBackground()).getCurrent();
            bitmap = Bitmap.createScaledBitmap(((BitmapDrawable) getBackground()).getBitmap(), 638, 638, true);
            width = getWidth();
            height = getHeight();
        }
        if (null == bitmap || x < 0 || y < 0 || x >= width || y >= height|| y>=bitmap.getHeight()|| x>=bitmap.getWidth()) {
            return false;
        }
        int pixel = bitmap.getPixel(x, y);
        if (Color.TRANSPARENT == pixel) {
            return false;
        }
        return super.onTouchEvent(event);
    }
}
